package cn.lngaosu.lwyd.module.integration.controller.admin.weixinvideolivedata;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.lngaosu.lwyd.framework.common.pojo.PageParam;
import cn.lngaosu.lwyd.framework.common.pojo.PageResult;
import cn.lngaosu.lwyd.framework.common.pojo.CommonResult;
import cn.lngaosu.lwyd.framework.common.util.object.BeanUtils;
import static cn.lngaosu.lwyd.framework.common.pojo.CommonResult.success;

import cn.lngaosu.lwyd.framework.excel.core.util.ExcelUtils;

import cn.lngaosu.lwyd.framework.apilog.core.annotation.ApiAccessLog;
import static cn.lngaosu.lwyd.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.lngaosu.lwyd.module.integration.controller.admin.weixinvideolivedata.vo.*;
import cn.lngaosu.lwyd.module.integration.dal.dataobject.weixinvideolivedata.WeixinvideoLivedataDO;
import cn.lngaosu.lwyd.module.integration.service.weixinvideolivedata.WeixinvideoLivedataService;

@Tag(name = "管理后台 - 微信视频号直播大屏数据")
@RestController
@RequestMapping("/integration/weixinvideo-livedata")
@Validated
public class WeixinvideoLivedataController {

    @Resource
    private WeixinvideoLivedataService weixinvideoLivedataService;

    @PostMapping("/create")
    @Operation(summary = "创建微信视频号直播大屏数据")
    @PreAuthorize("@ss.hasPermission('integration:weixinvideo-livedata:create')")
    public CommonResult<Long> createWeixinvideoLivedata(@Valid @RequestBody WeixinvideoLivedataSaveReqVO createReqVO) {
        return success(weixinvideoLivedataService.createWeixinvideoLivedata(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新微信视频号直播大屏数据")
    @PreAuthorize("@ss.hasPermission('integration:weixinvideo-livedata:update')")
    public CommonResult<Boolean> updateWeixinvideoLivedata(@Valid @RequestBody WeixinvideoLivedataSaveReqVO updateReqVO) {
        weixinvideoLivedataService.updateWeixinvideoLivedata(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除微信视频号直播大屏数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('integration:weixinvideo-livedata:delete')")
    public CommonResult<Boolean> deleteWeixinvideoLivedata(@RequestParam("id") Long id) {
        weixinvideoLivedataService.deleteWeixinvideoLivedata(id);
        return success(true);
    }

    @GetMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除微信视频号直播大屏数据")
                @PreAuthorize("@ss.hasPermission('integration:weixinvideo-livedata:delete')")
    public CommonResult<Boolean> deleteWeixinvideoLivedataList(@RequestParam("ids") List<Long> ids) {
        weixinvideoLivedataService.deleteWeixinvideoLivedataListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信视频号直播大屏数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('integration:weixinvideo-livedata:query')")
    public CommonResult<WeixinvideoLivedataRespVO> getWeixinvideoLivedata(@RequestParam("id") Long id) {
        WeixinvideoLivedataDO weixinvideoLivedata = weixinvideoLivedataService.getWeixinvideoLivedata(id);
        return success(BeanUtils.toBean(weixinvideoLivedata, WeixinvideoLivedataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信视频号直播大屏数据分页")
    @PreAuthorize("@ss.hasPermission('integration:weixinvideo-livedata:query')")
    public CommonResult<PageResult<WeixinvideoLivedataRespVO>> getWeixinvideoLivedataPage(@Valid WeixinvideoLivedataPageReqVO pageReqVO) {
        PageResult<WeixinvideoLivedataDO> pageResult = weixinvideoLivedataService.getWeixinvideoLivedataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WeixinvideoLivedataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信视频号直播大屏数据 Excel")
    @PreAuthorize("@ss.hasPermission('integration:weixinvideo-livedata:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWeixinvideoLivedataExcel(@Valid WeixinvideoLivedataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WeixinvideoLivedataDO> list = weixinvideoLivedataService.getWeixinvideoLivedataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信视频号直播大屏数据.xls", "数据", WeixinvideoLivedataRespVO.class,
                        BeanUtils.toBean(list, WeixinvideoLivedataRespVO.class));
    }

}